package api

import (
	"gitee.com/DengAnbang/Wefore/src/bean"
	"gitee.com/DengAnbang/Wefore/src/dbops"
	"gitee.com/DengAnbang/Wefore/src/fun"
	"gitee.com/DengAnbang/goutils/httpUtils"
	"net/http"
)

/**
* showdoc
* @catalog 接口文档/聊天相关
* @title 发送消息
* @description 发送消息的接口
* @method socket type=801
* @url /pc/message/chat/send
* @param formUserId 必选 string 发送人的id
* @param toUserId 必选 string 接收人的id
* @param content 必选 string 发送的内容
* @param fileId 选填 string 文件的id
* @param messageType 必选 string 消息的类型
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"发送成功"}
* @remark 最后修改时间:2018.12.07 10.30
* @number 7
 */
func ChatMessageSendHttp(_ http.ResponseWriter, r *http.Request) error {
	formUserId := httpUtils.GetValueFormRequest(r, "formUserId")
	toUserId := httpUtils.GetValueFormRequest(r, "toUserId")
	content := httpUtils.GetValueFormRequest(r, "content")
	fileId := httpUtils.GetValueFormRequest(r, "fileId")
	messageType := httpUtils.GetValueFormRequest(r, "messageType")
	err := dbops.ChatMessageSend(formUserId, toUserId, content, fileId, messageType)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("发送成功")

}
func ChatMessageSendSocket(parameters map[string]interface{}) error {
	formUserId := fun.GetString(parameters, "formUserId")
	toUserId := fun.GetString(parameters, "toUserId")
	content := fun.GetString(parameters, "content")
	fileId := fun.GetString(parameters, "fileId")
	messageType := fun.GetString(parameters, "messageType")
	err := dbops.ChatMessageSend(formUserId, toUserId, content, fileId, messageType)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("发送成功")
}

/**
* showdoc
* @catalog 接口文档/聊天相关
* @title 删除消息
* @description 删除消息的接口
* @method socket type=802
* @url /pc/message/chat/delete
* @param messageId 必选 string 消息的id
* @param fileId 选填 string 文件的id
* @return {"code":0,"type":0,"message":"","debug_message":"","data":"删除成功"}
* @remark 最后修改时间:2018.12.07 10.30
* @number 7
 */
func ChatMessageDeleteHttp(_ http.ResponseWriter, r *http.Request) error {
	messageId := httpUtils.GetValueFormRequest(r, "messageId")
	fileId := httpUtils.GetValueFormRequest(r, "fileId")
	err := dbops.ChatMessageDelete(messageId, fileId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")

}
func ChatMessageDeleteSocket(parameters map[string]interface{}) error {
	messageId := fun.GetString(parameters, "messageId")
	fileId := fun.GetString(parameters, "fileId")
	err := dbops.ChatMessageDelete(messageId, fileId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage("删除成功")
}

/**
* showdoc
* @catalog 接口文档/聊天相关
* @title 查看未读消息
* @description 查看未读消息的接口
* @method socket type=803
* @url /pc/message/chat/select/tome
* @param toUserId 必选 string 自己的id
* @return {"code":0,"type":0,"message":"","debug_message":"","data":[]ChatMessage}
* @remark 最后修改时间:2018.12.07 10.30
* @number 7
 */
func ChatMessageSelectToMeHttp(_ http.ResponseWriter, r *http.Request) error {
	toUserId := httpUtils.GetValueFormRequest(r, "toUserId")
	messages, err := dbops.ChatMessageSelectToMe(toUserId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(messages)

}
func ChatMessageSelectToMeSocket(parameters map[string]interface{}) error {
	toUserId := fun.GetString(parameters, "toUserId")
	messages, err := dbops.ChatMessageSelectToMe(toUserId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(messages)
}
